﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pavilion;

namespace Pavilion.Control
{
    public class IconSelector : Control
    {
        private Icon selectedIcon;

        public IconSelector(Rectangle bounds) : base()
        {
            AreaBounds = bounds;

            this.BackgroundColor = new Color(128, 128, 128);
        }
        
        public override void Draw()
        {
            if (!Visible)
                return;

            DrawManager.Instance.FillRectangleOnScreen(ScreenBounds, BackgroundColor);
            DrawManager.Instance.DrawRectangleOnScreen(ScreenBounds, OutlineColor);

            base.Draw();
        }

        public void AddIcon(Icon icon)
        {
            _Collection.Add(icon);
            icon.Selected += new Action<Icon>(Icon_Selected);

            if (selectedIcon == null)
                icon.Select();
        }

        public void InsertIcon(int index, Icon icon)
        {
            _Collection.Insert(index, icon);
            icon.Selected += new Action<Icon>(Icon_Selected);
        }

        public void RemoveIcon(Icon icon)
        {
            int index = IndexOfIcon(icon);

            _Collection.Remove(icon);

            if (selectedIcon == icon)
            {
                selectedIcon = null;

                if (index == Count)
                    this[index-1].Select();
                else
                    this[index].Select();
            }
        }

        public void RemoveIconAt(int index)
        {
            Icon icon = this[index];

            _Collection.RemoveAt(index);

            if (selectedIcon == icon)
            {
                selectedIcon = null;

                if (index == Count)
                    this[index - 1].Select();
                else
                    this[index].Select();
            }
        }

        public int IndexOfIcon(Icon icon)
        {
            return _Collection.IndexOf(icon);
        }

        public void ClearIcons()
        {
            _Collection.Clear();
            selectedIcon = null;
        }

        /// <summary>
        /// Gets the amount of icons that are within this collection.
        /// </summary>
        public int Count
        {
            get { return _Collection.Count; }
        }

        /// <summary>
        /// Gets or sets the icon that has been selected.
        /// </summary>
        public Icon SelectedIcon
        {
            get { return selectedIcon; }
            set
            {
                int i = IndexOfIcon(value);
                if (value != null && !_Collection.Contains(value))
                    return;

                if (selectedIcon != null)
                    selectedIcon.BackgroundColor = Color.Transparent;

                selectedIcon = value;
                
                if (selectedIcon != null)
                    selectedIcon.BackgroundColor = new Color(200, 200, 200);
            }
        }

        /// <summary>
        /// Gets or sets the selected index of this icon.
        /// </summary>
        public int SelectedIndex
        {
            get { return IndexOfIcon(selectedIcon); }
            set
            {
                if (Count == 0 || value == SelectedIndex)
                    return;

                if (value < 0)
                    SelectedIcon = this[0];
                else if (value < Count)
                    SelectedIcon = this[value];
                else
                    SelectedIcon = this[Count - 1];
            }
        }

        /// <summary>
        /// Gets or sets the value at the given index.
        /// </summary>
        /// <param name="index">An index within this array list.</param>
        public Icon this[int index]
        {
            get { return (Icon)_Collection[index]; }
            set
            {
                RemoveIconAt(index);
                InsertIcon(index, value);
            }
        }

        private void Icon_Selected(Icon icon)
        {
            SelectedIcon = icon;
        }
    }
}
